<?php require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['PAD'].'Modules/CentrodeCustos/modconf.php');
MYDCONN(); 
	$TOT = 0; 
	$TimeTime = "TimePay";
	$Search = 'WHERE ';
	if ($_POST['code'] != '') {$Search .= "'".$_POST['code']."' IN (a.CODE, b.CODE) AND ";}
  	if ($_POST['TimeType'] != '') {$TimeTime = $_POST['TimeType'];} 

  	if ($_POST['IReference'] != '') {
	$Search .= "ProvIReference = '".$_POST['IReference']."' AND ";
	}
  	if ($_POST['Invoice'] != '') {
	$Search .= "b.Ref = '".$_POST['Invoice']."' AND ";
	}
  	if ($_POST['Provider'] != '') {
	$Search .= "a.RefProvider = '".$_POST['Provider']."' IN (a.Provider, b.RefProvider) ";
	}
  	if ($_POST['Amount'] != '') {
	$Search .= "a.Amount = '".$_POST['Amount']."' AND ";
	}

  	if ($_POST['TimeEnd'] != '') {
		if ($TimeTime == 'TimeClose') {
	$Search .= "{$TimeTime} BETWEEN '".$_POST['TimeStart']." 00:00:00' AND '".$_POST['TimeEnd']." 23:59:59' AND ";
		
		} else {
	$Search .= "({$TimeTime} BETWEEN '".$_POST['TimeStart']." 00:00:00' AND '".$_POST['TimeEnd']." 23:59:59' OR {$TimeTime} = '0000-00-00 00:00:00') AND ";
		}
	}
  	if ($_POST['Status'] != '') {
	$Search .= "b.Status = ".$_POST['Status']." AND a.Status != 5";
	} else {
	$Search .= "b.Status != 5 AND a.Status != 5";
	}

   $GET = $GLOBALS['MYD']->db_array("SELECT 
					YEAR(b.Time) AS FactRef,
					b.RefProvider,
					b.ProvIReference,
					b.Ref,
					main_offices.name AS Office,
					main_users.name AS User,
					a.Description,
					a.Amount,
					DATE(b.{$TimeTime}) AS TimeMain,
					hcce_status.name AS Status,
					k.name AS Provider,
					k.Observations,
					hcce_paytypes.name AS Meth,
					a.CODE
				FROM ccenter_invarticles AS a
				INNER JOIN ccenter_invoices AS b ON b.Ref = a.RefInvoice
				INNER JOIN hcce_status ON hcce_status.StatRef = b.Status AND hcce_status.Type = 1
				LEFT OUTER JOIN hcce_paytypes ON hcce_paytypes.Ref = b.TYPEPay
				LEFT OUTER JOIN main_offices ON main_offices.ref = b.RefOffice
				LEFT OUTER JOIN main_users ON main_users.ref = b.RefUser
				LEFT OUTER JOIN ccenter_providers AS k ON k.Ref = b.RefProvider
				{$Search}
				ORDER BY b.Ref ASC"); 	?>
<table width="100%"  border="0" cellpadding="2" cellspacing="0">
  <tr bgcolor="#F2F2F2">
    <td width="5%">N.Interno </td>
    <td width="5%">N.Factura </td>
    <td width="25%" bgcolor="#F2F2F2"><p>ObservaÇ&otilde;es</p>    </td>
    <td width="23%">Fornecedor</td>
    <td width="5%">Natureza</td>
    <td width="8%" bgcolor="#F2F2F2">Valor</td>
    <td width="12%" bgcolor="#F2F2F2">Data</td>
    <td width="8%" bgcolor="#F2F2F2">Estado</td>
  </tr>
  <?php
	for ($f = 1; $f <= $GET[0]; $f++) { 
	$TOT += $GET[$f]['Amount'];
	$Provlen = strlen($GET[$f]['Provider']);
	
	if ($Provlen > 26) {
		$Prov = substr($GET[$f]['Provider'],0,26).'*';
		} else { 
		$Prov = $GET[$f]['Provider'];
		}
		echo '<tr height="20px" bgcolor="'.gridlines($f).'"><td>';
		mn_field('button','ED','extID',$GET[$f]['Ref'].'/'.$GET[$f]['FactRef'],'','','','" disabled readonly="');
		echo '</td>
		<td>'.$GET[$f]['ProvIReference'].'</td>
		<td title="'.$GET[$f]['Description'].'">'.substr($GET[$f]['Description'],0,30).'</td>
		<td title="'.$GET[$f]['Provider'].' ('.$GET[$f]['Observations'].')">'.substr($GET[$f]['Provider'],0,30).'</td>
		<td>'.$GET[$f]['CODE'].'</td>
		<td>'.$GET[$f]['Amount'].'</td>
		<td>'.$GET[$f]['TimeMain'].'</td>
		<td>';
		echo $GET[$f]['Status'];
		if ($GET[$f]['Meth'] != null) { 
		echo ' - '.$GET[$f]['Meth'];}
		echo '</td>
		</tr>';
   } 
   		echo '<tr height="20px" bgcolor="#D4D6E4">
		<td colspan="4" align="right"><b># '.$GET[0].'</b></td>
		<td></td>
		<td colspan="3"><b>'.$__sys['CurrencyISO'].' '.$TOT.'</b></td>
		</tr>'; ?>
</table>